package where2go.sctu.com.zhiyou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.adpater.RecyclerViewClassifyAdapter;
import where2go.sctu.com.zhiyou.adpater.RecyclerViewOneAdapter;
import where2go.sctu.com.zhiyou.db.ScrollLinearLayoutManager;
import where2go.sctu.com.zhiyou.entity.Classify;
import where2go.sctu.com.zhiyou.entity.PersonRecomClass;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.activity
 * Created by 张某人
 * CreatedTime:on 2018/5/16   14:13
 * Description:
 */

public class ClassifyDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mImageView;
    private RecyclerView mRecyclerView;
    private RecyclerViewClassifyAdapter mAdapter;
    private List<Classify.ClassifyData> dataList=new ArrayList<Classify.ClassifyData>();
    private String url="http://47.104.80.155/youyiyou/class/atype?type_Id=";

    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.classify_detail_layout);
        Intent intent=getIntent();
        String type_Id=intent.getStringExtra("typeId");
        initView();
        loaderData(url+type_Id+"&page=1");
    }

    private void initView() {

        mImageView=(ImageView) findViewById(R.id.classify_back);
        mImageView.setOnClickListener(this);
        mRecyclerView=(RecyclerView) findViewById(R.id.classify_recyclerView);

        mRecyclerView.setLayoutManager(new ScrollLinearLayoutManager(this,
                ScrollLinearLayoutManager.VERTICAL,false));
    }

    /**
     * 获取网络数据
     * @param url
     */
    private void loaderData(String url) {
        //        开始使用volley获取数据
//        第二部：选择请求对象
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {  //onPostExecute
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                final Classify classify = gson.fromJson(s, Classify.class);
                dataList = classify.getData();
                mAdapter = new RecyclerViewClassifyAdapter(dataList, ClassifyDetailActivity.this);
                mAdapter.setItemClickListener(new RecyclerViewClassifyAdapter.OnItemClickListener(){
                    @Override
                    public void onItemClick(View view, int position) {
                        //跳转到详情页
                        Intent intent=new Intent();
                        intent.setClass(ClassifyDetailActivity.this, LineDetailsActivity.class);
                        //用Bundle携带数据
                        Bundle bundle=new Bundle();
                        int classId=dataList.get(position).getClassId();
                        intent.putExtra("classId",classId);
                        bundle.putString("classId", String.valueOf(classId));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {

                    }
                });
                mRecyclerView.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("TAG", volleyError.toString());
                Toast.makeText(getApplicationContext(), "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
//        第三部：把请求对象加入到请求的队列当中
        });
        MyApplication.getHttpQueues().add(request);
    }

    /**
     * 点击返回
     * @param v
     */

    @Override
    public void onClick(View v) {
        finish();
    }
}
